package com.ruoyi.web.controller.UserRealMoney;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.ruoyi.web.controller.qqLoginAndPayByHTQ.QQPayExtract;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.User.domain.User;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.User.wxpay.WechatExtract;
import com.ruoyi.UserRealMoney.domain.UserRealmoney;
import com.ruoyi.UserRealMoney.service.IUserRealmoneyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.APPConstants;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.controller.PhoneLogin.PhoneLoginController;

/**
 * 用户提现 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-31
 */
@Controller
@RequestMapping("/UserRealMoney/UserRealMoneyVerify")
public class UserRealMoneyVerifyController extends BaseController
{
    private String prefix = "UserRealMoney/UserRealMoneyVerify";
	
	@Autowired
	private IUserRealmoneyService userRealmoneyService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private PhoneLoginController weixin;
	
	@RequiresPermissions("UserRealMoney:userRealmoney:view")
	@GetMapping()
	public String userRealmoney()
	{
	    return prefix + "/userRealmoney";
	}
	
	/**
	 * 查询用户提现列表
	 */
	@RequiresPermissions("UserRealMoney:userRealmoney:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(UserRealmoney userRealmoney)
	{
		userRealmoney.setState(0);
		startPage();
        List<UserRealmoney> list = userRealmoneyService.selectUserRealmoneyList(userRealmoney);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出用户提现列表
	 */
	@RequiresPermissions("UserRealMoney:userRealmoney:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserRealmoney userRealmoney)
    {
    	List<UserRealmoney> list = userRealmoneyService.selectUserRealmoneyList(userRealmoney);
        ExcelUtil<UserRealmoney> util = new ExcelUtil<UserRealmoney>(UserRealmoney.class);
        return util.exportExcel(list, "userRealmoney");
    }
	
	/**
	 * 新增用户提现
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户提现
	 */
	@RequiresPermissions("UserRealMoney:userRealmoney:add")
	@Log(title = "用户提现", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(UserRealmoney userRealmoney)
	{		
		return toAjax(userRealmoneyService.insertUserRealmoney(userRealmoney));
	}

	/**
	 * 修改用户提现
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		UserRealmoney userRealmoney = userRealmoneyService.selectUserRealmoneyById(id);
		mmap.put("userRealmoney", userRealmoney);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存用户提现
	 */
	@RequiresPermissions("UserRealMoney:userRealmoney:edit")
	@Log(title = "用户提现", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(UserRealmoney userRealmoney)
	{		
		return toAjax(userRealmoneyService.updateUserRealmoney(userRealmoney));
	}
	
	/**
	 * 删除用户提现
	 */
	@RequiresPermissions("UserRealMoney:userRealmoney:remove")
	@Log(title = "用户提现", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(userRealmoneyService.deleteUserRealmoneyByIds(ids));
	}
	
	/**
	 * 确认同意提现
	 * @throws ParseException 
	 */
	@PostMapping("/updateStatus")
	@ResponseBody
	public AjaxResult updateStatus(String id) throws ParseException {
		String toUserSuccess = "提现已到账  请注意查收";
		int realId = Integer.parseInt(id);
		UserRealmoney persist = userRealmoneyService
				.selectUserRealmoneyById(realId);
		Double money = persist.getMoney();
		BigDecimal toReal = new BigDecimal(money);
		int userId = persist.getUserId();
		User user = userService.selectUserById(userId);
		Double creditD = persist.getAccount();
		BigDecimal credit = new BigDecimal(creditD);
		Double example = credit.subtract(toReal).doubleValue();
		credit = new BigDecimal(example);
		user.setCredit1(credit);
		persist.setVerifyDate(DateTools.getYMDhms());
		persist.setState(1);
		persist.setAccount(example);
		if ("微信钱包".equals(persist.getOutWay())) {
			String trueJine = String.valueOf(money);
			String openId = user.getOpenidWa();
			String desc = "提现";
			String realMoneyId = persist.getId().toString();
			try {
				String a = WechatExtract.deptToUser(trueJine, openId, desc,
						realMoneyId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}						
		}
		if ("QQ钱包".equals(persist.getOutWay())) {
			String trueJine = String.valueOf(money);
			String openId = user.getOpenidWa();
			String desc = "提现";
			String realMoneyId = persist.getId().toString();
			try {
				String a = QQPayExtract.deptToUser(trueJine, openId, desc, realMoneyId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		weixin.fxx_now(String.valueOf(userId), persist.getId().toString(), toUserSuccess);
		return toAjax(userRealmoneyService.updateUserRealmoney(persist));
	}
	
	/**
	 * 确认不同意提现
	 * @throws ParseException 
	 */
	@PostMapping("/updateStatus2")
	@ResponseBody
	public AjaxResult updateStatus2(String id) throws ParseException {
		String toUserFail = "提现未通过  请注意查收";
		int realId=Integer.parseInt(id);
		UserRealmoney persist=userRealmoneyService.selectUserRealmoneyById(realId);	
		Double money=persist.getMoney();
		BigDecimal toReal=new BigDecimal(money);
		int userId=persist.getUserId();
		User user=userService.selectUserById(userId);
		BigDecimal credit=user.getCredit1();
		Double setBackMoney=credit.add(toReal).doubleValue();
		user.setCredit1(new BigDecimal(setBackMoney));
		userService.updateUser(user);
		persist.setVerifyDate(DateTools.getYMDhms());
		persist.setState(-1);		
		weixin.fxx_now(String.valueOf(userId), persist.getId().toString(), toUserFail);
		return toAjax(userRealmoneyService.updateUserRealmoney(persist));
	}
	
}
